﻿
using System;

namespace WindowsAPI.Service
{
    internal class LazyValue<T>
    {
        /* ------------------------------------------------------------------------------------------ */

        private object f_Value;
        private readonly Func<T> f_Initializer;

        /* ------------------------------------------------------------------------------------------ */

        public T Value { get { return (T)(f_Value ?? (f_Value = f_Initializer())); } }

        public bool IsHasValue { get { return !ReferenceEquals(f_Value, null); } }

        /* ------------------------------------------------------------------------------------------ */

        public LazyValue(Func<T> Initializer)
        {
            if(Initializer == null)
                throw new ArgumentNullException(nameof(Initializer));
            f_Initializer = Initializer;
        }

        /* ------------------------------------------------------------------------------------------ */

        public static implicit operator T(LazyValue<T> value) { return value.Value; }

        /* ------------------------------------------------------------------------------------------ */
    }
}
